package com.srit.yoggsaron.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.srit.yoggsaron.common.JsonPageResult;
import com.srit.yoggsaron.common.JsonQuery;
import com.srit.yoggsaron.common.JsonResult;
import com.srit.yoggsaron.db.model.Task;
import com.srit.yoggsaron.db.model.TaskStatusEnum;
import com.srit.yoggsaron.db.model.TaskView;
import com.srit.yoggsaron.support.TaskSupport;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(tags = "任务相关信息接口")
@RestController
@RequestMapping(value = "/task")
@PropertySource(value="classpath:application.properties" , encoding = "UTF-8")
public class TaskController {
	private final Logger logger = LoggerFactory.getLogger(TaskController.class);
	@Autowired
	private TaskSupport _taskSupport=null;
	@ApiOperation(value = "添加任务信息接口", httpMethod = "PUT", notes = "添加任务信息")
	@PostMapping(value = "/add")
	@ResponseBody
	public   JsonResult<Long >  add(@RequestBody  Task task  ) {
		    
		try
		{
			if(task==null)
				throw new Exception("要添加的被任务为空");
			Long  result = _taskSupport.insert( task );
			return  new JsonResult< Long>( result).success();
		}
		catch (Exception e) {
			logger.debug(e.toString(),e);
			logger.error(e.toString(),e);
			return new JsonResult<Long>(0L).fail(-1 , "添加被任务失败:  "+e.getMessage() );
		}
	}
	
	
	@ApiOperation(value = "删除被任务接口", 
			httpMethod = "DELETE" ,
			notes = "删除被任务" 
			)
	@ApiImplicitParams({
		@ApiImplicitParam(name="jsonQuery",value="被任务对象",required=true      ) 
	})
	@PostMapping(value = "/remove")
	@ResponseBody
	public  JsonResult<Long > remove ( @RequestBody  JsonQuery jsonQuery   ) {
		try
		{
			if(jsonQuery==null)
				throw new Exception("要删除的被任务条件为空");
			Long  result = _taskSupport.delete(  jsonQuery );
			return  new JsonResult< Long>( result).success();
		}
		catch (Exception e) {
			logger.debug(e.toString(),e);
			logger.error(e.toString(),e);
			return new JsonResult<Long>(0L).fail(-1 , "删除被任务失败"+e.getMessage() );
		}
	}
	
	
	
	
	
	@PostMapping(value = "/complate")
	@ResponseBody
	public  JsonResult<Long > complate ( @RequestBody  JsonQuery jsonQuery   ) {
		try
		{
			if(jsonQuery==null)
				throw new Exception("要删除的被任务条件为空");
			List<Task>  list = _taskSupport.query(   jsonQuery );
			long result=0;
			for(Task task : list)
			{
				if( task.getUserId()!=null && task.getStartTime()!=null && task.getEndTime()==null )
				{
					Task newTask =new Task();
					newTask.setTaskId( task.getTaskId() );
					newTask.setUpdateTime(new Date());
					newTask.setEndTime(new Date());
					result+=_taskSupport.update( newTask );
				}
			}
			
			return  new JsonResult< Long>( result).success();
		}
		catch (Exception e) {
			logger.debug(e.toString(),e);
			logger.error(e.toString(),e);
			return new JsonResult<Long>(0L).fail(-1 , "删除被任务失败"+e.getMessage() );
		}
	}
	
	@PostMapping(value = "/start")
	@ResponseBody
	public  JsonResult<Long > start ( @RequestBody  JsonQuery jsonQuery   ) {
		try
		{
			if(jsonQuery==null)
				throw new Exception("要启动的任务为空");
			List<Task>  list = _taskSupport.query(   jsonQuery );
			long result=0;
			for(Task task : list)
			{
				if( task.getUserId()!=null && task.getEndTime()==null )
				{
					Task newTask =new Task();
					newTask.setTaskId( task.getTaskId() );
					newTask.setUpdateTime(new Date());
					newTask.setStartTime( new Date());
					result+=_taskSupport.update( newTask );
				}
			}
			
			return  new JsonResult< Long>( result).success();
		}
		catch (Exception e) {
			logger.debug(e.toString(),e);
			logger.error(e.toString(),e);
			return new JsonResult<Long>(0L).fail(-1 , "删除被任务失败"+e.getMessage() );
		}
	}
	
	
	@ApiOperation(value = "修改被任务接口", 
			httpMethod = "POST" ,
			notes = "修改被任务" 
			)
	@ApiImplicitParams({
		@ApiImplicitParam(name="task",value="被任务对象",required=true      ) 
	})
	@PostMapping(value = "/update")
	@ResponseBody
	public  JsonResult<Long > update ( @RequestBody  Task task   ) {
		try
		{
			if(task==null|| task.getTaskId() ==null)
				throw new Exception("要修改的被任务为空或任务ID为空");
			Long  result = _taskSupport.update( task );
			return  new JsonResult< Long>( result).success();
		}
		catch (Exception e) {
			logger.debug(e.toString(),e);
			logger.error(e.toString(),e);
			return new JsonResult<Long>(0L).fail(0 , "修改被任务失败"+e.getMessage() );
		}
	}
	
	
	
	@ApiOperation(value = " 查询接口", 
			httpMethod = "POST" ,
			notes = "查询任务" 
			)
	@ApiImplicitParams({
		@ApiImplicitParam(name="jsonQuery",value="被任务对象",required=true      ) 
	})
	@PostMapping(value = "/query")
	@ResponseBody
	public  JsonResult<List<Task> > query ( @RequestBody   JsonQuery jsonQuery   ) {
		try
		{
			List<Task>  result = _taskSupport.query( jsonQuery );
			return  new JsonResult< List<Task>>( result).success();
		}
		catch (Exception e) {
			logger.debug(e.toString(),e);
			logger.error(e.toString(),e);
			return new JsonResult<List<Task>>(null).fail(-1 , "查询任务失败"+e.getMessage() );
		}
	}
	@ApiOperation(value = " 查询户带分页接口", 
			httpMethod = "POST" ,
			notes = "查询带分页任务" 
			)
	@ApiImplicitParams({
		@ApiImplicitParam(name="jsonQuery",value="被任务对象",required=true      ) 
	})
	@PostMapping(value = "/page")
	@ResponseBody
	public  JsonResult<List<Task> > page ( @RequestBody   JsonQuery jsonQuery   ) {
		try
		{
			Long count =  _taskSupport.count( jsonQuery );
			List<Task>  result = _taskSupport.page( jsonQuery );
			return  new JsonPageResult< Task>( count ,result).success();
		}
		catch (Exception e) {
			logger.debug(e.toString(),e);
			logger.error(e.toString(),e);
			return new JsonResult<List<Task>>(null).fail(-1 , "查询任务失败"+e.getMessage() );
		}
	}
	@ApiOperation(value = " 查询视图接口", 
			httpMethod = "POST" ,
			notes = "查询视图任务" 
			)
	@ApiImplicitParams({
		@ApiImplicitParam(name="jsonQuery",value="被任务对象",required=true      ) 
	})
	@PostMapping(value = "/queryView")
	@ResponseBody
	public  JsonResult<List<TaskView> > queryView ( @RequestBody   JsonQuery jsonQuery   ) {
		try
		{
			List<TaskView>  result = _taskSupport.queryView( jsonQuery );
			return  new JsonResult< List<TaskView>>( result).success();
		}
		catch (Exception e) {
			logger.debug(e.toString(),e);
			logger.error(e.toString(),e);
			return new JsonResult<List<TaskView>>(null).fail(-1 , "查询任务失败"+e.getMessage() );
		}
	}
	@ApiOperation(value = " 查询户带分页接口", 
			httpMethod = "POST" ,
			notes = "查询带分页任务" 
			)
	@ApiImplicitParams({
		@ApiImplicitParam(name="jsonQuery",value="被任务对象",required=true      ) 
	})
	@PostMapping(value = "/pageView")
	@ResponseBody
	public  JsonResult<List<TaskView> > pageView ( @RequestBody   JsonQuery jsonQuery   ) {
		try
		{
			Long count =  _taskSupport.viewCount( jsonQuery );
			List<TaskView>  result = _taskSupport.viewPage( jsonQuery );
			return  new JsonPageResult< TaskView>( count ,result).success();
		}
		catch (Exception e) {
			logger.debug(e.toString(),e);
			logger.error(e.toString(),e);
			return new JsonResult<List<TaskView>>(null).fail(-1 , "查询任务失败"+e.getMessage() );
		}
	}
	
	
	
	
	
	
	@ApiOperation(value = " 查询任务状态", 
			httpMethod = "POST" ,
			notes = "查询任务状态" 
			)
	@PostMapping(value = "/status")
	@ResponseBody
	public  JsonResult<List<ExtendAttribute> > status (@RequestBody   JsonQuery jsonQuery ) {
		try
		{
			List<TaskStatusEnum> list  =  Task.getStatus() ;
			List<ExtendAttribute> attributeList = new ArrayList<ExtendAttribute>();
			
			for(TaskStatusEnum re:list)
			{
				ExtendAttribute at = new ExtendAttribute();
				at.setName( re.getText());
				at.setValue(re.getValue()+"");
				attributeList.add(at);
			}
			return  new JsonResult< List<ExtendAttribute>>(  attributeList).success();
		}
		catch (Exception e) {
			logger.debug(e.toString(),e);
			logger.error(e.toString(),e);
			return new JsonResult< List<ExtendAttribute>>(null).fail(-1 , "查询角色失败"+e.getMessage() );
		}
	}
}
